package com.example.tb.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseResult<T> {

    private Integer code;

    private String message;

    private T data;

    /**
     * 成功返回无数据
     *
     * @return
     */
    public static <V> ResponseResult<V> success() {
        return new ResponseResult<>(ResponseCode.SUCCESS.code, ResponseCode.SUCCESS.message);
    }

    /**
     * 成功返回
     *
     * @param data 返回数据
     * @return
     */
    public static <V> ResponseResult<V> success(V data) {
        return new ResponseResult<>(ResponseCode.SUCCESS.code, ResponseCode.SUCCESS.message, data);
    }

    /**
     * 成功返回
     *
     * @param str 返回数据
     * @return
     */
    public static <V> ResponseResult<V> success(String str,V data) {
        return new ResponseResult<>(ResponseCode.SUCCESS.code, str,data);
    }

    public static <V> ResponseResult<V> success(String str) {
        return new ResponseResult<>(ResponseCode.SUCCESS.code, str);
    }

    /**
     * 失败返回
     *
     * @param errorInfo 错误信息
     * @return
     */
    public static <V> ResponseResult<V> error(ResponseCode errorInfo) {
        return new ResponseResult<>(errorInfo.code, errorInfo.message);
    }

    /**
     * 失败返回
     *
     * @param str 自定义错误信息
     * @return
     */
    public static <V> ResponseResult<V> error(String str) {
        return new ResponseResult<>(ResponseCode.ERROR.code, str);
    }

    /**
     * 失败返回
     *
     * @return
     */
    public static <V> ResponseResult<V> error(Integer code, String message, V data) {
        return new ResponseResult<>(code, message, data);
    }

    /**
     * 失败返回
     *
     * @return
     */
    public static <V> ResponseResult<V> error(ResponseCode responseCode, String message, V data) {
        return new ResponseResult<>(responseCode.code, message, data);
    }

    public ResponseResult(ResponseCode responseCode, T data) {
        this.code = responseCode.code;
        this.message = responseCode.message;
        this.data = data;
    }

    public ResponseResult(T data) {
        ResponseCode success = ResponseCode.SUCCESS;
        this.code = success.code;
        this.message = success.message;
        this.data = data;
    }

    public ResponseResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResponseResult(ResponseCode responseCode) {
        this.code = responseCode.code;
        this.message = responseCode.message;
    }

    public ResponseResult(ResponseCode responseCode, String message) {
        this.code = responseCode.code;
        this.message = message;
    }


    @Getter
    public enum ResponseCode {

        /**
         * 枚举为状态码和错误信息
         */
        SUCCESS(200, "success"),

        SUCCESS_HEART(202, null),

        ERROR(400, "请求错误"),

        NO_AUTH(401, "没有权限"),

        NOT_FOUND(404, "接口不存在"),

        TIME_OUT(408, "请求超时"),

        TOKEN_EXPIRED(411, "登录过期"),

        USER_ACCOUNT_FREEZE(411, "你的账号已被冻结"),

        USER_ACCOUNT_LOGOUT(411, "你的账号已注销"),

        DATA_FORMAT_ERROR(412, "数据格式错误"),

        SQL_INSERT_ERROR(413, "插入数据异常"),

        SQL_UPDATE_ERROR(414, "更新数据异常"),

        PHONE_ERROR(400, "手机号码无效"),

        PHONE_CODE_EXPIRED(400, "验证码无效或过期"),

        PHONE_CODE_ERROR(400, "验证码错误"),

        PHONE_HAVE_REGISTERED(400, "手机号码已被注册"),

        PARAM_ERROR(400, "参数错误"),

        OPERATION_ERROR(400, "操作过于频繁"),

        PARAMETRIC_NULL_ANOMALY(412, "参数为空异常"),

        USER_ID_ERROR(500, "用户id缺失异常"),

        SERVER_ERROR(500, "服务器异常");


        Integer code;
        String message;

        ResponseCode(Integer code, String message) {
            this.code = code;
            this.message = message;
        }
    }
}